package com.xinlong.dao;

import com.xinlong.entity.Member;
import com.xinlong.entity.Route;
import com.xinlong.entity.Vehicle;
import com.xinlong.entity.product;
import org.apache.ibatis.annotations.*;
import java.util.Map;

import java.util.List;

public interface Iproductdao {

    @Select("select * from product where id=#{id}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "vehicleid",property = "vehicleid"),
            @Result(column = "id", property = "route",javaType = List.class, many = @Many(select = "com.xinlong.dao.RouteMapper.selById")),
            @Result(column = "vehicleid", property = "vehicle",javaType = Vehicle.class, one = @One(select = "com.xinlong.dao.IVehicledao.selectVehicleById")),
    })
    product findById(String id);

    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "id", property = "route",javaType = List.class, many = @Many(select = "com.xinlong.dao.RouteMapper.selById")),
    })
    @Select("select * from product where productStatus=#{status}")
    List<product> findByproductStatus(@Param("status") Integer status);

    @Select("select * from product")
    List<product> productSelect() throws Exception;

    @Insert("insert into product(id,productNum,productName,cityName,endTime,departureTime,productPrice,productDesc,productStatus,vehicleid) values(null,#{productNum},#{productName},#{cityName},#{endTime},#{departureTime},#{productPrice},#{productDesc},#{productStatus},#{vehicleid})")
    void save(product produc);


    @Delete("delete from product where id=#{id}")
    void delproduct(Integer id);

    @Update("update product set productNum=#{productNum},productName=#{productName},cityName=#{cityName},endTime=#{endTime},departureTime=#{departureTime},productPrice=#{productPrice},productStatus=#{productStatus},departureTime=#{departureTime},vehicleid=#{vehicleid} where id=#{id}")
    void upproduct(product product);


    @Select("update product p set p.productStatus = 2 where  now() > p.departureTime and NOW() < p.endTime")
    Integer updateStatus2();

    @Select("update product p set p.productStatus =3 where now() >  p.endTime")
    Integer updateStatus3();

    @Select("select productPrice from product where id = #{id}")
    Integer selProductPrice(@Param("id") int id);

    @Select("select count(t.id) count,v.number number \n" +
            "from orders o \n" +
            "left join member m \n" +
            "on (m.id=o.memberId) \n" +
            "left join traveller t \n" +
            "on (t.memberId=m.id) \n" +
            "left join product p \n" +
            "on (p.id = o.productId) \n" +
            "left join vehicle v \n" +
            "on (v.id=p.vehicleId)\n" +
            "where p.id = #{productId}")
    Map<String,Object> selProductNumber(int productId);

}
